//
//  ViewController.swift
//  Reader
//
//  Created by MN on 2023/10/26.
//  https://github.com/dengzemiao/DZMeBookRead.git

import UIKit

class TxtViewController: BaseViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "小说"
        addWifiBtn()
        loadFiles(type: .txt)
    }
    
    func addWifiBtn() {
        let rightItem = UIBarButtonItem(title: "wifi", style: .plain, target:self , action: #selector(wifiAction(_ : )))
        navigationItem.rightBarButtonItem = rightItem
        
        let leftItem = UIBarButtonItem(title: "刷新", style: .plain, target:self , action: #selector(reloadAction(_ : )))
        navigationItem.leftBarButtonItem = leftItem
    }
    // 按钮点击事件
    @objc func wifiAction(_ sender : UIBarButtonItem) {
        navigationController?.pushViewController(WifiUploadController(), animated: true);
    }
    @objc func reloadAction(_ sender : UIBarButtonItem) {
        loadFiles(type: .txt)
        tableView.reloadData()
    }
    override func openFile(url: URL?) {
        
//        let url = Bundle.main.url(forResource: "求魔", withExtension: "txt")
        DZMReadTextParser.parser(url: url) { [weak self] (readModel) in
            let vc  = DZMReadController()
            vc.readModel = readModel
            vc.hidesBottomBarWhenPushed = true
            self?.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

